<?
ob_start();
session_start();
$keyword		= $HTTP_GET_VARS['keyword'];
$show			= $HTTP_GET_VARS['show'];
$pendidikan		= $HTTP_GET_VARS['pendidikan'];
$pengalaman		= $HTTP_GET_VARS['pengalaman'];
$skill			= $HTTP_GET_VARS['skill'];
$fungsional		= $HTTP_GET_VARS['fungsional'];
$status			= $HTTP_GET_VARS['status'];
$key			= $HTTP_GET_VARS['key'];
$st				= $HTTP_GET_VARS['st'];
$log_id			= $HTTP_GET_VARS['log_id'];
$divisi			= $HTTP_GET_VARS['divisi'];

	include("config.php");

// cek hak akses untuk cabang
$sad = mssql_query("SELECT nama_menu FROM VPrivUser A, VCabang B WHERE username='".$HTTP_SESSION_VARS['user_login']."' AND baca='1' AND A.nama_menu=B.Kode_Cabang",$conn);
while($rad = mssql_fetch_array($sad)){
// VARIABLE UNTUK CABANG
$VAreaCode[] = $rad[0];
}

	echo"<table class='table_indent2' width='100%' border=1>
	<tr style='font-weight: bold;'>
	 <td>No</td>
	 <td>NIK</td>
	 <td>Nama</td>
	 <td>Biro/Divisi/Cabang</td>
	 <td>Jabatan</td>
	 <td>Fungsional</td>
	 <td>Golongan</td>
	 <td>Status Karyawan</td>
	 <td>Status Kerja</td>
	 <td>Pendidikan</td>
	 <td>Jurusan</td>
	</tr>
	";
	if($HTTP_GET_VARS['st'] == "Pelamar"){
			// jika pelamar yang dipilih
			$sql = "WITH limit AS (SELECT ROW_NUMBER() OVER (ORDER BY NIK) AS No, * FROM Karyawan WHERE (Nama LIKE '%$keyword%' OR NIK LIKE '%$keyword%' OR Alamat LIKE '%$keyword%') AND pelamar='1') SELECT * FROM limit";
		}
		else{
			$sql = "WITH limit AS (SELECT ROW_NUMBER() OVER (ORDER BY NIK) AS No, * FROM VLatestKarir A, MstSO B WHERE A.kode_so2=B.kode_so AND (Nama LIKE '%$keyword%' OR NIK LIKE '%$keyword%' OR Alamat LIKE '%$keyword%') AND B.AreaCode IN (";

			for($k=0;$k<count($VAreaCode);$k++){
				$sql .= "'$VAreaCode[$k]',";
			}
			$sql .= "'$VAreaCode[0]') ";

			if(!empty($HTTP_GET_VARS['st'])){
				if($HTTP_GET_VARS['st'] == "Berhenti"){
					$sql .= " AND Jenis IN ('U','H') ";			
				}
				else{
					if($HTTP_GET_VARS['st'] == "all"){
						// no command
					}
					else{
						$sql .= " AND Jenis NOT IN ('U','H') AND (stat_kar1 = '".$HTTP_GET_VARS['st']."' OR stat_kar2='".$HTTP_GET_VARS['st']."') ";
					}
				}
			}
			
			if(!empty($HTTP_GET_VARS['divisi'])){
				if($HTTP_GET_VARS['divisi'] == "all"){
				}
				else{
					$sql .= " AND ( substring(kode_so1,1,7)='".substr($HTTP_GET_VARS['divisi'],0,7)."' OR substring(kode_so2,1,7)='".substr($HTTP_GET_VARS['divisi'],0,7)."' )";
				}
			}
			
			$sql .= " ) SELECT * FROM limit ";
		}

	$sqlxx = mssql_query($sql,$conn);
	while($row = mssql_fetch_array($sqlxx)){
		echo"
		<tr class='contentlist'>
		 <td>$row[No]</td>
		 <td>$row[NIK]</a></td>
		 <td>$row[Nama]</td>
		 ";
		 
		$sqlx	= mssql_query("SELECT * FROM VLatestKarir WHERE NIK='$row[NIK]'",$conn);
		$rowx 	= mssql_fetch_array($sqlx);
				
		//if(empty($rowx[kode_so1])){
			$kodeso = $rowx[kode_so2];
		//}
		//else{
		//	$kodeso = $rowx[kode_so1];
		//}
		
		if(empty($kodeso)){
			echo"<td>&nbsp;</td>";
		}
		else{
			$s1 = mssql_query("SELECT * FROM VLevelCabang WHERE substring(kode_so,1,7)='".substr($kodeso,0,7)."'", $conn);
			if(mssql_num_rows($s1) > 0){
				$r1 = mssql_fetch_array($s1);
				if(!empty($r1[uraian])){
					echo"<td>$r1[uraian]</td>";
				}
				else{
					echo"<td>&nbsp;</td>";
					echo"<td>&nbsp;</td>";
				}			
			}
			else{
					echo"<td>&nbsp;</td>";
			}
		}	
		// get jabatan
		
		$jabatan = $rowx[jabatan2];
		if(!empty($jabatan)){
			$sjab = mssql_query("SELECT * FROM MstJabatan WHERE kode='$jabatan'");
			$rjab = mssql_fetch_array($sjab);
			echo"<td>$rjab[jabatan]</td>";
		}
		else{
			echo"<td>&nbsp;</td>";
		}
		
		// get fungsional
		
		$fungsi = $rowx[fungsi2];
		if(!empty($fungsi)){
			$sfug = mssql_query("SELECT * FROM MstFungsional WHERE kode='$fungsi'");
			$rfug = mssql_fetch_array($sfug);
			echo"<td>$rfug[nama]</td>";
		}
		else{
			echo"<td>&nbsp;</td>";
		}
		
		// get golongan
		
		echo"<td>$rowx[gol2]</td>";
		
		// get status karyawan
		if(empty($rowx[stat_kar1])){
			$status = $rowx[stat_kar2];
		}
		else{
			$status = $rowx[stat_kar1];
		}

		$s4 = mssql_query("SELECT * FROM VStatusKaryawan WHERE kode='".$status."'",$conn);
		$r4 = mssql_fetch_array($s4);
		if(!empty($r4[Nama])){
			echo"<td>$r4[Nama]</td>";
		}
		else{
			echo"<td>&nbsp;</td>";
		}
		
		// get status kerja
		
		if($rowx[Jenis] == "U" || $rowx[Jenis] == "H"){
			echo"<td>Berhenti</td>";
		}
		else{
			if($row[pelamar] == "1"){
				echo"<td>Pelamar</td>";
			}
			else{
				echo"<td>Bekerja</td>";
			}
		}
			
		// get pendidikan
	$sqlpend = mssql_query("	
	select NIK, A.Pend_ID, CASE keterangan 
		WHEN 'SD' THEN '1' 
		WHEN 'SD Lain-lain' THEN '1' 
		WHEN 'SMP' THEN '2' 
		WHEN 'SMP Lain-lain' THEN '2' 
		WHEN 'SMA' THEN '3' 
		WHEN 'SMK' THEN '3' 
		WHEN 'SMEA' THEN '3' 
		WHEN 'STM' THEN '3' 
		WHEN 'SMKK' THEN '3' 
		WHEN 'ST' THEN '3' 
		WHEN 'D1' THEN '4' 
		WHEN 'D2' THEN '5' 
		WHEN 'D3' THEN '6' 
		WHEN 'D4' THEN '7' 
		WHEN 'S1' THEN '8' 
		WHEN 'S2' THEN '9' 
		WHEN 'S3' THEN '10' 
	END jenjang, keterangan, jurusan  from pendidikan A, mstpendidikan B where A.Pend_ID=B.Pend_ID AND NIK='$row[NIK]' ORDER BY jenjang DESC",$conn);
	$rowpend = mssql_fetch_array($sqlpend);
	echo"<td>$rowpend[keterangan]</td>";
	echo"<td>$rowpend[jurusan]</td>";
			echo"
		</tr>
		";
	}
		echo"</table>";
///*		
		// export to excel
	header('Content-type: application/vnd.ms-excel');
	header("Content-Disposition: attachment; filename=DownloadMasterKaryawan.xls");
	header("Pragma: no-cache");
	header("Expires: 0");
//*/
?>